package me.firstapp.service.bbs.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.utils.NotificationJson;
import me.firstapp.common.utils.Page;
import me.firstapp.module.bbs.BbsNotification;
import me.firstapp.module.bbs.ShopNotification;
import me.firstapp.module.bbs.SystemNotification;
import me.firstapp.repository.bbs.BbsNotificationRepository;
import me.firstapp.repository.bbs.ShopNotificationRepository;
import me.firstapp.repository.bbs.SystemNotificationRepository;
import me.firstapp.service.bbs.NotificationService;

@Service
public class NotificationServiceImpl implements NotificationService {

	@Autowired
	private BbsNotificationRepository bbsNotificationRepository;

	@Autowired
	private ShopNotificationRepository shopNotificationRepository;

	@Autowired
	private SystemNotificationRepository systemNotificationRepository;

	@Override
	public Long findMemberUnReadBbsNotificationCounts(Long memberId, Long apiClientId) {
		return bbsNotificationRepository.findMemberUnReadBbsNotificationCounts(memberId, apiClientId);
	}

	@Override
	public Page<BbsNotification> findMemberBbsNotificationPage(Integer pageNo, Integer pageSize, Long memberId,
			Integer isRead, Long apiClientId) {
		if (memberId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "用户id");
		}
		Page<BbsNotification> bbsNotificationPage = bbsNotificationRepository.findMemberBbsNotificationPage(pageNo,
				pageSize, memberId, isRead, apiClientId);

		return bbsNotificationPage;
	}

	@Override
	public NotificationJson findUnReadNotificationCounts(Long memberId, Long apiClientId) {
		if (memberId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "用户id");
		}
		NotificationJson notificationJson = new NotificationJson();
		notificationJson.setBbs(bbsNotificationRepository.findMemberUnReadBbsNotificationCounts(memberId, apiClientId));
		notificationJson
				.setShop(shopNotificationRepository.findMemberUnReadShopNotificationCounts(memberId, apiClientId));
		notificationJson.setSystem(
				systemNotificationRepository.findMemberUnReadSystemNotificationCounts(memberId, apiClientId));
		return notificationJson;
	}

	@Override
	public Page<SystemNotification> findSysNotificationPage(Integer pageNo, Integer pageSize, Long memberId,
			Integer isRead, Long apiClientId) {
		return systemNotificationRepository.findMemberSystemNotificationPage(pageNo, pageSize, memberId, isRead,
				apiClientId);
	}

	@Override
	public void executeSetNotificationReaded(Long memberId, String type, Long apiClientId) {
		if (memberId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "用户id");
		}
		switch (type) {
		case "bbs":
			List<BbsNotification> bbsNotificationList = bbsNotificationRepository
					.findUnReadBbsNotificationListByMemberId(memberId, apiClientId);
			for (BbsNotification bbsNotification : bbsNotificationList) {
				bbsNotification.setIsRead(1);
				bbsNotificationRepository.update(bbsNotification);
			}
			break;
		case "shop":
			List<ShopNotification> shopNotificationList = shopNotificationRepository
					.findUnReadShopNotificationListByMemberId(memberId, apiClientId);
			for (ShopNotification shopNotification : shopNotificationList) {
				shopNotification.setIsRead(1);
				shopNotificationRepository.update(shopNotification);
			}
			break;
		case "system":
			List<SystemNotification> systemNotificationList = systemNotificationRepository
					.findUnReadSystemNotificationListByMemberId(memberId, apiClientId);
			for (SystemNotification systemNotification : systemNotificationList) {
				systemNotification.setIsRead(1);
				systemNotificationRepository.update(systemNotification);
			}
			break;
		}
	}

	@Override
	public Page<ShopNotification> findShopNotificationPage(Integer pageNo, Integer pageSize, Long memberId,
			Integer isRead, Long apiClientId) {
		return shopNotificationRepository.findMemberShopNotificationPage(pageNo, pageSize, memberId, isRead,
				apiClientId);
	}

}
